<!-- Sofija Puric 6/08 -->
<?php

include_once dirname(__FILE__) . '/staticStars.php';
require_once dirname(__FILE__) . '/db.php';
require_once dirname(__FILE__) . '/con_hostel_star.function.php';

function writeStars($HostelID) {
    $query = "SELECT (avg(Security)+avg(Location)+avg(Service)+avg(Fun)+avg(Cleanliness)+avg(Price))/6 AS prosek " .
            "FROM feedback WHERE IDHostel = $HostelID;";
    $conn = dbConnect("sib");
    $result = mysql_query($query, $conn);
    $row = mysql_fetch_array($result);
    $prosek = $row["prosek"];
    $ss = new staticStars(round($prosek));
    return $ss->printStars($prosek);
}

$con = dbConnect("sib");

$sql = "SELECT IDHostel, Name, Description,
    (rank_security + rank_location + rank_service + rank_fun + rank_cleanliness + rank_price)/6 AS rank_all
    FROM hostelrank ORDER BY rank_all DESC LIMIT 3";

$result = mysql_query($sql, $con);
if (!$result)
    die('Error: ' . mysql_error());
$row1 = mysql_fetch_array($result);
$row2 = mysql_fetch_array($result);
$row3 = mysql_fetch_array($result);

$hid = $row1["IDHostel"];
$result_photo = mysql_query("SELECT * FROM photo WHERE IDHostel='$hid' AND IndexPhoto='1'", $con);
if (!$result_photo)
    die('Error: ' . mysql_error());
$row_photo = mysql_fetch_array($result_photo);

$hid2 = $row2["IDHostel"];
$result_photo2 = mysql_query("SELECT * FROM photo WHERE IDHostel='$hid2' AND IndexPhoto='1'", $con);
if (!$result_photo2)
    die('Error: ' . mysql_error());
$row_photo2 = mysql_fetch_array($result_photo2);

$hid3 = $row3["IDHostel"];
$result_photo3 = mysql_query("SELECT * FROM photo WHERE IDHostel='$hid3' AND IndexPhoto='1'", $con);
if (!$result_photo3)
    die('Error: ' . mysql_error());
$row_photo3 = mysql_fetch_array($result_photo3);


echo '

<h2>Top hostels</h2>



<div id="firsthostel">

  <div id="hosteltitle">
    <h2>' . $row1["Name"] . '</h2>
  </div>
  <div id="hostelrank">';

echo printHostelStars($row1["IDHostel"]);

echo ' </div>
  <div id="hostelphoto">
  <a href = "hostel.php?hid=' . $row1["IDHostel"] . '"> <img src="' . $row_photo["Url"] . '" alt="' . $row_photo["Alt"] . '"/></a>
  </div>
  <div id="hostelcontent">
    <p>' . $row1["Description"] . '</p>
  </div>
    <div class="links"><a href="hostel.php?hid=' . $row1["IDHostel"] . '">See more</a></div>
  
</div>
<div id="hostelbottom">
  &nbsp;
  </div>

<div class="hostel">
  <div class="title">
      <h2>' . $row2["Name"] . '</h2>
  </div>
  <a href = "hostel.php?hid=' . $row2["IDHostel"] . '"><img src="' . $row_photo2["Url"] . '" alt="' . $row_photo2["Alt"] . '"/></a>
 <div class="rate_choice hostels_stars">';
$idh = $row2["IDHostel"];
echo writeStars($idh);
echo '  </div>
                    <div class="entry">' . $row2["Description"] . '</div>';
echo '
    <div class="links"><a href="hostel.php?hid=' . $row2["IDHostel"] . '">See more</a></div>
</div>

<div class="hostel">
  <div class="title">
      <h2>' . $row3["Name"] . '</h2>
  </div>
  <a href = "hostel.php?hid=' . $row3["IDHostel"] . '"><img src="' . $row_photo3["Url"] . '" alt="' . $row_photo3["Alt"] . '"/></a>
  <div class="rate_choice hostels_stars">';
$idh = $row3["IDHostel"];
echo writeStars($idh);
echo '  </div>
                    <div class="entry">' . $row3["Description"] . '</div>';
echo '
    <div class="links"><a href="hostel.php?hid=' . $row3["IDHostel"] . '">See more</a></div>
</div>

';

mysql_close($con);
?>
